package mysql

import (
	"gorm.io/gorm"
	"manuscript-service/servicerpc/servicerpc"
)

type Files struct {
	gorm.Model
	Name    string `gorm:"type:varchar(50);not null;comment:'稿件名称'"`
	Context string `gorm:"type:varchar(200);not null;comment:'内容'"`
	Image   string `gorm:"type:varchar(255);not null;comment:'稿件图片'"`
	ClassId int64  `gorm:"type:int(11);not null;comment:'分类ID'"`
}

func (f *Files) FileAdd(db *gorm.DB) error {
	return db.Debug().Create(f).Error
}

func (f *Files) FindFilesByName(db *gorm.DB, name string) error {
	return db.Debug().Where("name = ?", name).First(f).Error
}

func FileList(db *gorm.DB, list []*servicerpc.FileList) []*servicerpc.FileList {

	db.Debug().Model(&Files{}).Select(
		"files.id as files_id",
		"files.name as files_name",
		"files.context",
		"files.image",
		"classes.name as class_name",
	).Joins("left join classes on classes.id = files.class_id").Find(&list)
	return list

}
